//
//  TabFrameController.m
//  newTabbar
//
//  Created by xfttt on 14-6-3.
//  Copyright (c) 2014年 xftt. All rights reserved.
//

#import "TabFrameController.h"

#define kFrameAnimationDuration 0.4

@interface TabFrameController ()
@end

@implementation TabFrameController
- (void)setViewControllers:(NSArray *)newViewControllers
{
	_viewControllers = [newViewControllers copy];
    
	[self loadFrame];
    [self setContentViewFrame];
    [self setTabViewFrame];
}
- (void)setTabViewFrame
{
    NSInteger barheight = 0;
    if([UIDevice currentDevice].systemVersion.floatValue >= 7.0)
    {
        barheight = 20;
    }
    
    CGFloat x,y,w,h;
    x = 0;
    y = barheight;
    w = tabView.frame.size.width;
    h = tabView.frame.size.height;
    tabView.frame = CGRectMake(x, y, w, h);
}
- (void)setContentViewFrame
{
    NSInteger barheight = 0;
    if([UIDevice currentDevice].systemVersion.floatValue >= 7.0)
    {
        barheight = 20;
    }
    CGFloat x,y,w,h;
    x = tabView.frame.size.width;
    y = barheight;
    w = 1024-tabView.frame.size.width;
    h = tabView.frame.size.height;
    contentView.frame = CGRectMake(x, y, w, h);
    if (contentView.subviews.count) {
        ((UIView*)contentView.subviews[0]).frame = contentView.bounds;
    }
}
- (void)loadFrame
{
    if (!_isInitialized) {
        if([UIDevice currentDevice].systemVersion.floatValue >= 7.0)
        {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        }
        self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        contentView = [[UIView alloc] init];
        tabView = self.tabViewController.view.subviews[0];
        [self setContentViewFrame];
        [self setTabViewFrame];
        [self.view addSubview:tabView];
        [self.view addSubview:contentView];
        [self.tabViewController setTabFrameController:self];
        for (UIViewController *viewController in self.viewControllers)
        {
            [viewController setTabFrameController:self];
        }
        NSUInteger lastIndex = _selectedIndex;
        _selectedIndex = NSNotFound;
        self.selectedIndex = lastIndex;
        _isInitialized = YES;
    }
}
- (void)setSelectedIndex:(NSUInteger)newSelectedIndex
{
	if ([self.delegate respondsToSelector:@selector(TabFrameController:shouldSelectViewController:atIndex:)]){
		UIViewController *toViewController = (self.viewControllers)[newSelectedIndex];
		if (![self.delegate TabFrameController:self shouldSelectViewController:toViewController atIndex:newSelectedIndex])
			return;
	}
    if (_selectedIndex != newSelectedIndex)
	{
        UIViewController *fromViewController;
		UIViewController *toViewController;
        if (_selectedIndex != NSNotFound){
			fromViewController = self.selectedViewController;
		}
		_selectedIndex = newSelectedIndex;
		if (_selectedIndex != NSNotFound){
			toViewController = self.selectedViewController;
		}
		if (toViewController == nil){
			[fromViewController.view removeFromSuperview];
		}
        else
        {
            if (fromViewController != nil) {
                [fromViewController.view removeFromSuperview];
            }
            toViewController.view.frame = contentView.bounds;
            [contentView addSubview:toViewController.view];
            if ([self.delegate respondsToSelector:@selector(TabFrameController:didSelectViewController:atIndex:)])
                [self.delegate TabFrameController:self didSelectViewController:toViewController atIndex:newSelectedIndex];
        }
    }else{
        UIViewController *ViewC = self.selectedViewController;
        if ([self.delegate respondsToSelector:@selector(TabFrameController:didSelectOldViewController:atIndex:)])
            [self.delegate TabFrameController:self didSelectOldViewController:ViewC atIndex:newSelectedIndex];
    }
}
- (UIViewController *)selectedViewController
{
	if (self.selectedIndex != NSNotFound)
		return (self.viewControllers)[self.selectedIndex];
	else
		return nil;
}
- (void)setSelectedViewController:(UIViewController *)newSelectedViewController
{
	NSUInteger index = [self.viewControllers indexOfObject:newSelectedViewController];
	if (index != NSNotFound)
		[self setSelectedIndex:index];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}@end
#import <objc/runtime.h>
static void * tabFrameControllerKey = (void *)@"tabFrameControllerKey";
@implementation UIViewController (TabFrame)
@dynamic tabFrameController;
- (TabFrameController *)tabFrameController{
    return objc_getAssociatedObject(self, tabFrameControllerKey);
}
- (void)setTabFrameController:(TabFrameController *)tabFrameController{
    objc_setAssociatedObject(self, tabFrameControllerKey, tabFrameController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
